package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.bean.ClassShopBeen;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/3/28 0028
 * description：
 */
public class XFDShopAdapterGVAdapter extends BaseAdapter {

    private Context mContext;
    private List<ClassShopBeen.DataBean.GoodsBean> mList;

    private ImageView ivShopGood;
    private TextView tvPrice;

    public XFDShopAdapterGVAdapter(Context mContext, List<ClassShopBeen.DataBean.GoodsBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size()<4?mList.size():3;
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = View.inflate(mContext, R.layout.item_xfd_shop_adapter, null);

        ivShopGood = view.findViewById(R.id.iv_shop_good);
        tvPrice = view.findViewById(R.id.tv_price);
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback( R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(i).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into( ivShopGood);
        if (Double.parseDouble(mList.get(i).getDiscountprice())==0){
            tvPrice.setText("¥"+mList.get(i).getMarketprice());
        }else {
            tvPrice.setText("¥"+mList.get(i).getDiscountprice());
        }

        ivShopGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()){
                Intent intent=new Intent(mContext, XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id",mList.get(i).getId()+"");
                mContext.startActivity(intent);}
            }
        });
        return view;
    }
}
